package org.child_motivation.presentation.control;

import no.kantega.publishing.security.data.User;
import org.child_motivation.business.model.Group;
import org.child_motivation.business.service.GroupService;
import org.child_motivation.presentation.control.util.ControllerHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

@Controller
public class GroupController {

    @Autowired
    GroupService groupService;

    @RequestMapping(value="/group"  , method = RequestMethod.GET)
    public String  getGroup(@RequestParam Integer groupid, ModelMap model, HttpServletRequest request){
        Group group = groupService.getGroup(groupid);
        model.put("group", group);
        return "json/group";
    }



    @RequestMapping(value="/creategroup", method = RequestMethod.GET)
    public String getGroup(@ModelAttribute("group")Group group, ModelMap model, HttpServletRequest request){
        return "group/creategroup";
    }

    @RequestMapping(value="/creategroup", method = RequestMethod.POST)
    public String  createGroup(@ModelAttribute("group")Group group,ModelMap model, HttpServletRequest request){
        User user = ControllerHelper.getUserFromRequest(request);
        group.setOwner(user);
        groupService.saveOrUpdate(group);
        return "redirect:frontpage";
    }
}
